Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)


問題描述

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)

我的應用程序包含一個 Web 表單,有人可能會在其中提取一些信息。發生這種情況時,我會根據內容多次加載一個用戶控件,該控件上有一個 ImageButton。

由於這是在頁面加載後加載的,我怎樣才能讓點擊事件正常工作。由於頁面加載期間需要設置點擊事件。

示例場景:

Main.aspx

強>

<form id="form1" runat="server">
    <div>
        <asp:Button ID="clicker" runat="server" Text="Click Me" />
        <asp:PlaceHolder ID="PHwfuc" runat="server"></asp:PlaceHolder>
        <asp:Label runat="server" ID="ResponseMessage"></asp:Label>
    </div>
</form>

Main.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    clicker.Click += new EventHandler(this.ButClick);
}

protected void ButClick(object sender, EventArgs e)
{
    PlaceHolder placeHolder = new PlaceHolder();

    for (int i = 0; i < 2; i++)
    {
        WFUC1 test = LoadControl("~/WebFormUserControl/WFUC1.ascx") as WFUC1;
        test.Ident = i;
        placeHolder.Controls.Add(test);
    }

    PHwfuc.Controls.Add(placeHolder);
}

WFUC1.ascx

<asp:PlaceHolder runat="server" ID="DelAddrBtn"></asp:PlaceHolder>
<asp:Label runat="server" ID="ResponseMessage"></asp:Label>
<br />

WFUC1.ascx.cs

public WFUC1()
{
    TrashIcon = new ImageButton
    {
        AlternateText = "Delete Address",
        ImageUrl = "/images/trash.png",
        ToolTip = "Delete Address",
    };

    TrashIcon.Style.Add("cursor", "pointer");
    TrashIcon.Style.Add("width", "24px");
}

private ImageButton TrashIcon { get; set; }

public int Ident { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    TrashIcon.ID = $"Delete_{Ident}";
    TrashIcon.Click += new ImageClickEventHandler(this.TrashIcon_Click);
    DelAddrBtn.Controls.Add(TrashIcon);
}

protected void TrashIcon_Click(object sender, ImageClickEventArgs e)
{
    ResponseMessage.Text = $"Use Control Got it. {Ident}";
}

為 Rango 編輯<


參考解法

方法 1:

Seems, I have to reload all the controls on the main again to get the click event to execute. I accidentally made this work.

Below Project Visual Studio 2017 ‑ No binaries, except for one image and the rest is only project code.

Main.aspx

<form id="form1" runat="server">
    <asp:Button ID="clicker" runat="server" Text="Click Me" />
    <asp:PlaceHolder ID="PHwfuc" runat="server"></asp:PlaceHolder>
    <asp:Label runat="server" ID="ResponseMessage"></asp:Label>
</form>

Main.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    clicker.Click += new EventHandler(this.ButClick);

    if(ViewState["ButClick"] != null)
        LoadData();
}

protected void ButClick(object sender, EventArgs e)
{
    LoadData();
}

private void LoadData()
{
    PlaceHolder placeHolder = new PlaceHolder();

    for (int i = 0; i < 2; i++)
    {
        WFUC1 test = LoadControl("~/WebFormUserControl/WFUC1.ascx") as WFUC1;
        test.Ident = i;
        placeHolder.Controls.Add(test);
    }

    PHwfuc.Controls.Add(placeHolder);
    ViewState["ButClick"] = true;
}

WFUC1.ascx

<asp:ImageButton runat="server" ID="TrashIcon" ImageUrl = "/images/trash.png" ToolTip = "Delete Address" OnClick="TrashIcon_Click" />
<br />
<asp:Label runat="server" ID="ResponseMessage"></asp:Label>

WFUC1.ascx

public int Ident { get; set; }

public void TrashIcon_Click(object sender, ImageClickEventArgs e)
{
    ResponseMessage.Text = $"Use Control Got it. {Ident}";
}

(by ChizlChizl)

參考文件

  1. Web Form User Control Event, needs to be added after page loads (CC BY‑SA 2.5/3.0/4.0)

#ASP.NET






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論